<?php
namespace app\index\controller;

use app\BaseController;
use think\facade\View;

class Apitest extends Front
{
    public function index()
    {
        return View::fetch('index');
    }
    
    public function test()
    {
    //	var_dump($_SERVER);
    //	echo $_SERVER['REQUEST_METHOD'];
    	echo '<br>overload-method:';
    	$method = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'];
    	echo $method;
    	echo '<br>overload-url:';
    	$url = $_SERVER['HTTP_X_HTTP_URL_OVERRIDE'];
    	echo $url;
    	echo '<br>post:';
    	var_dump($_POST);
    	echo '<br>put:';
    	$putdata = file_get_contents("php://input");
    	var_dump($putdata);
    	$_method = $_SERVER['REQUEST_METHOD'];
    	if($_method == 'POST'){
    		$params = $_POST;
    	}
    	if($_method == 'GET'){
    		$params = $_GET;
    	}

    	$data = $this->http($url, $params, $_method);
    	echo $data;
    	die;
    }
	/**
	 * [http 调用接口函数]
	 * @param  string       $url     [接口地址]
	 * @param  array        $params  [数组]
	 * @param  string       $method  [GET\POST\DELETE\PUT]
	 * @param  array        $header  [HTTP头信息]
	 * @param  integer      $timeout [超时时间]
	 * @return [type]                [接口返回数据]
	 */
	function http($url, $params, $method = 'GET', $header = array(), $timeout = 5)
	{
	    // POST 提交方式的传入 $params 必须是字符串形式
	    $opts = array(
	        CURLOPT_TIMEOUT => $timeout,
	        CURLOPT_RETURNTRANSFER => 1,
	        CURLOPT_SSL_VERIFYPEER => false,
	        CURLOPT_SSL_VERIFYHOST => false,
	        CURLOPT_HTTPHEADER => $header
	    );

	    /* 根据请求类型设置特定参数 */
	    switch (strtoupper($method)) {
	        case 'GET':
	            $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
	            break;
	        case 'POST':
	            $params = http_build_query($params);
	            $opts[CURLOPT_URL] = $url;
	            $opts[CURLOPT_POST] = 1;
	            $opts[CURLOPT_POSTFIELDS] = $params;
	            break;
	        case 'DELETE':
	            $opts[CURLOPT_URL] = $url;
	            $opts[CURLOPT_HTTPHEADER] = array("X-HTTP-Method-Override: DELETE");
	            $opts[CURLOPT_CUSTOMREQUEST] = 'DELETE';
	            $opts[CURLOPT_POSTFIELDS] = $params;
	            break;
	        case 'PUT':
	            $opts[CURLOPT_URL] = $url;
	            $opts[CURLOPT_POST] = 0;
	            $opts[CURLOPT_CUSTOMREQUEST] = 'PUT';
	            $opts[CURLOPT_POSTFIELDS] = $params;
	            break;
	        default:
	            throw new Exception('不支持的请求方式！');
	    }
	  
	    /* 初始化并执行curl请求 */
	    $ch = curl_init();
	    curl_setopt_array($ch, $opts);
	    $data = curl_exec($ch);
	    $error = curl_error($ch);
	    return $data;
	}
}
